<?php
namespace app\controller\admin\system;

use app\BaseController;
use app\common\repositories\system\SystemCityRepository as repository;
use think\App;
use think\facade\Cache;

class City extends BaseController
{
    /**
     * @var repository
     */
    protected $repository;

    /**
     * Config constructor.
     * @param App $app
     * @param repository $repository
     */
    public function __construct(App $app, repository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }


    public function lst()
    {
        return app('json')->success($this->repository->getFormatList([['is_show', '=', 1],['level','<',2]]));
    }

    public function search()
    {
        $keyword = input('keyword');
        $query = $this->repository->getSearch();
        if(!$keyword || $keyword == '省' || $keyword == '县' || $keyword == '市') return success([]);
        $query = $query->where('name','like',"%{$keyword}%");
        $list = $query->group('name')->order("level")->field('city_id,name,merger_name,level')->select()->toArray();
        return success($list);
    }



    /**
     * @return mixed
     * User: xiest
     * Date: 2021/10/12
     */
    public function getlist()
    {
        $hasAll = input('has_all',0) == 1?1:0;

        $key = 'system_city_list_all_77'.$hasAll;

        if(Cache::has($key))
            $data = Cache::get($key);
        else{
            $data = $this->repository->getApiLst(['is_show' => 1],$hasAll);
            Cache::set($key,$data,3600*24*2);
        }

        //$data = $this->repository->getApiLst(['is_show' => 1],true);
        return app('json')->success($data);
    }

    public function cateLst()
    {
        return app('json')->success($this->repository->getCateList([['is_show', '=', 1],['level','<',3]]));
    }

}
